package dao;

import entity.User;
import tool.JNDIUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TransferDAO {

    public boolean transfer(User uer) {
        boolean changepower = false;
        int id = uer.getId();//得到该用户的id
        Connection connection = null;
        ResultSet resultSet1 = null;
        ResultSet resultSet2 = null;
        PreparedStatement preparedStatement1 = null;
        PreparedStatement preparedStatement2 = null;
        PreparedStatement preparedStatement3 = null;
        PreparedStatement preparedStatement4 = null;
        PreparedStatement preparedStatement5 = null;
        PreparedStatement preparedStatement6 = null;
        try {
            connection = JNDIUtils.getConnection();//建立连接
            connection.setAutoCommit(false);
            //sql语句
            String sql1 = "select nickname,level from  group_User"
                   +"where group_id = ？";
            preparedStatement1 = connection.prepareStatement(sql1);
            preparedStatement1.setInt(1, id);
            resultSet1 = preparedStatement1.executeQuery();
            //得到表中的nickname和level
            String nickname = null;
            String level = null;
            if (resultSet1.next()) {
                nickname = resultSet1.getString(1);
                level = resultSet1.getString(2);
            }
            //查找该组员组长 有点累赘 不知有无简单的
            String sql6 = "select owner from group_connection where member_id1= ? " +
                    "or member_id2 = ? or member_id3 = ? or member_id4 = ? or member_id5 = ? " +
                    " or member_id6 = ? or member_id7 = ? or member_id8 = ? or member_id9 = ? or " +
                    "member_id10 = ?";
            preparedStatement6 = connection.prepareStatement(sql6);
            //循环添加
            for(int i =1; i<=10;i++) {
                preparedStatement6.setInt(i,id);
            }
            resultSet2 = preparedStatement6.executeQuery();
            String Group_owner = null;
            if(resultSet2.next()){
                Group_owner = resultSet2.getString(1);
                System.out.println(Group_owner);
            }
            //判断此用户是否为组长
            if (level.equals("1")) {
                //修改成员为组长user表
                String sql2 = "update Group_User set level =5 where id = ? ";
                preparedStatement2 = connection.prepareStatement(sql2);
                preparedStatement2.setInt(1, id);

                //user表 组长变为组员
                String sql3 = "update Group_User set level =1 where nickname = ? ";
                preparedStatement3 = connection.prepareStatement(sql3);
                preparedStatement3.setString(1, Group_owner);

                //information表 修改owner信息
                String sql4 = "update Group_Information set owner = ? where owner = ? ";
                preparedStatement4 = connection.prepareStatement(sql4);
                preparedStatement4.setString(1, nickname);
                preparedStatement4.setString(2, Group_owner);

                //修改connection表的owner
                String sql5 = "update Group_Relation set owner = ? where owner = ? ";
                preparedStatement5 = connection.prepareStatement(sql5);
                preparedStatement5.setString(1, nickname);
                preparedStatement5.setString(2, Group_owner);

                preparedStatement2.executeUpdate();
                preparedStatement3.executeUpdate();
                preparedStatement4.executeUpdate();
                preparedStatement5.executeUpdate();
                connection.commit();
                changepower = true;
                System.out.println("修改成功");
            } else {
                System.out.println("此用户不是组员");
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();//错了就回滚
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            JNDIUtils.release(resultSet1, preparedStatement1, connection);//关闭资源
            JNDIUtils.release(preparedStatement2, null);
            JNDIUtils.release(preparedStatement3, null);
            JNDIUtils.release(preparedStatement4, null);
            JNDIUtils.release(preparedStatement5, null);
            JNDIUtils.release(resultSet2,preparedStatement6,null);
        }
       return changepower;

    }
}

